package com.rockcent.service.impl;

import com.rockcent.common.domain.enumclass.Whether;
import com.rockcent.helper.SysMessageHelper;
import com.rockcent.mall.lib.constants.SysMessageConstants;
import com.rockcent.mall.lib.domain.CustomerCouponWallet;
import com.rockcent.mall.lib.domain.MerchantCoupon;
import com.rockcent.mall.lib.domain.SysMessage;
import com.rockcent.mall.lib.domain.enumclass.CustomerCouponWalletStatus;
import com.rockcent.mall.lib.domain.enumclass.MerchantCouponStatus;
import com.rockcent.mall.lib.domain.enumclass.SysMessageType;
import com.rockcent.mall.lib.dto.IntegrationCouponRespDto;
import com.rockcent.mall.lib.repository.CustomerCouponWalletRepository;
import com.rockcent.mall.lib.repository.MerchantCouponRepository;
import com.rockcent.service.MerchantCouponSrv;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.text.MessageFormat;
import java.util.List;

/**
 * Created by zhangjh on 2016/11/29.
 */
@Service
public class MerchantCouponSrvImpl implements MerchantCouponSrv {
    private Logger log = LoggerFactory.getLogger(MerchantCouponSrvImpl.class);

    @Autowired
    private MerchantCouponRepository merchantCouponRepository;

    @Autowired
    private CustomerCouponWalletRepository customerCouponWalletRepository;

    @Override
    public List<Long> findAllExpiredCouponIds() {
        return merchantCouponRepository.findAllExpiredCouponIds();
    }

    @Override
    public void updateCoupoStatus(List<Long> ids, MerchantCouponStatus status) {
        if (null != ids) {
            for (Long couponId : ids) {
                updateCoupoStatus(couponId, status);
            }
        }
    }

    @Override
    public void updateCoupoStatus(Long couponId, MerchantCouponStatus status) {
        MerchantCoupon coupon = merchantCouponRepository.findOne(couponId);
        List<CustomerCouponWallet> couponWallets = customerCouponWalletRepository.findByCouponId(couponId);
        coupon.setStatus(status);
        if (null != couponWallets && !couponWallets.isEmpty()) {
            for (CustomerCouponWallet couponWallet : couponWallets) {
                couponWallet.setStatus(CustomerCouponWalletStatus.EXPIRED);
                customerCouponWalletRepository.save(couponWallet);
            }
        }
        String content = SysMessageConstants.SYS_COUPON_EXPIRED.getContent();
        content = MessageFormat.format(content, coupon.getName());

        //广播消息
        try {
            String frontedUrl = SysMessageConstants.SYS_COUPON_EXPIRED.getFrontendUrl();
            frontedUrl = MessageFormat.format(frontedUrl, coupon.getName());
            SysMessage message = SysMessageHelper.SINGLETON.createMsgBean(SysMessageConstants.SYS_COUPON_EXPIRED, SysMessageType.OP, content);
            message.setFrontendUrl(frontedUrl);
            //广播消息
            SysMessageHelper.SINGLETON.pushMsgByMallAndMerchantId(message, null, coupon.getMerchantId());
        } catch (Exception e) {
            log.error("卡券过期 sendMessage fail:", e);
        }
        merchantCouponRepository.save(coupon);
    }

    @Override
    public BigDecimal queryAvailableAmountByMallId(Long mallId, Long channelId) {
        return merchantCouponRepository.queryAvailableAmountByMallId(mallId, channelId);
    }

    @Override
    public List<IntegrationCouponRespDto> findByIntegrationAndIntegrationChannelIdAndStatus(Whether yes, Long integrationChannelId, MerchantCouponStatus status) {
        return merchantCouponRepository.findByIntegrationAndIntegrationChannelIdAndStatus(yes, integrationChannelId, status);
    }

    @Override
    public IntegrationCouponRespDto findTop1ByIntegrationAndIntegrationChannelIdAndStatusAndFaceVal(Whether yes, Long integrationChannelId, MerchantCouponStatus issued, BigDecimal faceVal) {
        IntegrationCouponRespDto respDto = merchantCouponRepository.findOneByIntegrationAndIntegrationChannelIdAndStatusAndFaceVal(yes, integrationChannelId, issued, faceVal);
        return respDto;
    }

    @Override
    public List<Long> findAllReceiveOutCouponIds() {


        return null;
    }


}
